﻿using System;
using System.Collections;
using System.Collections.Generic;
using OWL.Rendering.HRP;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Rendering;

public class RenderSceneLight : MonoBehaviour
{
    public GameObject directionalLight;
    public Vector3 angleOffset1;
    public Vector3 angleOffset2;

    public Vector3 angleFixed;
    
    // 渲染设置组件
    private List<HSceneRenderSettingsComponent> RenderSettingsComponents;
    // 灯光组件
    private List<Light> RenderLights;
    // 后效组件
    private List<Volume> RenderVolumes;

    private void SaveLightings()
    {
        var renderSetting = GetComponent<HBaseRenderScene>();
        RenderSettingsComponents = renderSetting.RenderSettingsComponents;
        RenderLights = renderSetting.RenderLights;
        RenderVolumes = renderSetting.RenderVolumes;
        renderSetting.RenderSettingsComponents = new List<HSceneRenderSettingsComponent>();
        renderSetting.RenderLights = new List<Light>();
        renderSetting.RenderVolumes = new List<Volume>();
    }

    private void ApplyLightings()
    {
        var renderSetting = GetComponent<HBaseRenderScene>();
        renderSetting.RenderSettingsComponents = RenderSettingsComponents;
        renderSetting.RenderLights = RenderLights;
        renderSetting.RenderVolumes = RenderVolumes;
    }
    
    public void SaveOffsetBack()
    {
        #if UNITY_EDITOR
        SaveLightings();
        var rpgCamera = GameObject.Find("RpgCamera(Clone)");
        if (rpgCamera != null)
        {
            var camera = rpgCamera.transform.Find("Camera");
            if (camera)
            {
                angleOffset1 = directionalLight.transform.eulerAngles - camera.eulerAngles;
                var path = "Assets/Arts/Scene/RPG/LightSettings/RpgRenderScene.prefab";
                UnityEditor.PrefabUtility.SaveAsPrefabAsset(this.gameObject, path);
            }
        }
        ApplyLightings();
        #endif
    }
    
    public void SaveOffsetNormal()
    {
#if UNITY_EDITOR
        SaveLightings();
        var rpgCamera = GameObject.Find("RpgCamera(Clone)");
        if (rpgCamera != null)
        {
            var camera = rpgCamera.transform.Find("Camera");
            if (camera)
            {
                angleOffset2 = directionalLight.transform.eulerAngles - camera.eulerAngles;
                var path = "Assets/Arts/Scene/RPG/LightSettings/RpgRenderScene.prefab";
                UnityEditor.PrefabUtility.SaveAsPrefabAsset(this.gameObject, path);
            }
        }
        ApplyLightings();
#endif
    }
    
}
